﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Spore.Validator
{
    /// <summary>
    /// 中间值验证器，验证字段是否大于等于最小值并小于等于最大值
    /// </summary>
   public class ValidatorIsInRange:ValidatorBase
    {
        /// <summary>
        /// 最小值
        /// </summary>
        private double minValue;
        /// <summary>
        /// 最大值
        /// </summary>
        private double maxValue;

        /// <summary>        
        /// </summary>
        /// <param name="minValue">下限值</param>
        /// <param name="maxValue">上限值</param>
        /// <param name="errMsg">验证未通过时错误提示信息</param>
        public ValidatorIsInRange(double minValue, double maxValue, string errMsg)
            : base(errMsg)
        {
           
            this.minValue = minValue ;
             this.maxValue = maxValue;
        }

        public ValidatorIsInRange(int minValue, int maxValue)
        {
            this.minValue = minValue;
            this.maxValue = maxValue;
            this.ErrorMessage = string.Format("值不能大于{0},不能小于{1}",maxValue.ToString(),minValue.ToString());
        }

        /// <summary>
        /// 验证控件内容是否在指定长度范围内
        /// </summary>
        /// <param name="controlToValidate">待验证控件</param>
        /// <returns>如果在范围内,返回true;否则返回false</returns>
        public override bool IsValid(Control controlToValidate)
        {
            double temp;
            if (double.TryParse(controlToValidate.Text.Trim(), out temp))
            {
                return (temp >= minValue) & (temp <= maxValue);
            }
            return false;
        }
    }
}
